#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include "libcytovision.h"

/*! \brief Security checks for directories
 *  \param s Path to be verified
 *
 * Avoid a lot of security problems checking if the library itself made any attempts
 * to access beyond the cases jail.
 *
 *  \retval 0 Aproved
 *  \retval -1 Security threat
*/
int cytovision_check_security(char *s) {
  int len=strlen(s);

  if(strcmp(s,"~")==0) goto error;
  if(strcmp(s,"..")==0) goto error;
  if(strncmp(s,"../",3)==0) goto error;
  if(strstr(s,"/../")!=NULL) goto error;
  if(len>4) {
    if(strncmp(&s[len-4],"/..",3)==0) goto error;
  }
  return 0;
error:
    fprintf(stderr,"Invalid filename %s\n",s);
    return -1;
}

void cytovision_debug(int level, char *str, ...) {
  if(level<=DEBUGLEVEL) {
    va_list ap;

    va_start(ap,str);
    vprintf(str,ap);
    va_end(ap);
  }
}
